<?php

/*
 * Project: oCoder Framework
 * File: /classes/helper.php
 * Purpose: class which build a common function
 * Author: Trung Hoang
 */

class Helper {

    //get current page url
    function currentPageURL() {
        $curpageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {
            $curpageURL.= "s";
        }
        $curpageURL.= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $curpageURL.= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
        } else {
            $curpageURL.= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
        }
        return $curpageURL;
    }

    //check login
    static function isLogin() {
        if (isset($_SESSION['userLogin'])) {
            return true;
        } else {
            return false;
        }
    }

    //get username login
    static function getUsernameLogin() {
        if (isset($_SESSION['userLogin'])) {
            return $_SESSION['userLogin']->username;
        } else {
            return null;
        }
    }

    //convert Date in VN to Date in sql
    static function convertDate($date) {
        $epl = explode("/", $date);
        return $epl[2] . "-" . $epl[1] . "-" . $epl[0];
    }

    //convert Date in mysql to Date in VN
    static function reConvertDate($date) {
        $epl = explode("-", $date);
        return $epl[2] . "/" . $epl[1] . "/" . $epl[0];
    }

    static function getFirstImage($content) {
        $r = array();
        preg_match_all("/<img alt=\"(.*)\" src=\"(.*)\" /Ui", $content, $temp);


        for ($i = 0; $i < count($temp[2]); $i++) {
            $hdrs = @get_headers($temp[2][$i]);
            $kq = is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/', $hdrs[0]) : false;
            if ($kq) {
                $imgPATH = $temp[2][$i];
                $alt = htmlentities($temp[1][$i]);
                array_push($r, $imgPATH, $alt);
                return $r;
            }
        }
        $imgPATH = PATH_IMAGE . "no-image.png";
        $alt = "No image";
        array_push($r, $imgPATH, $alt);
        return $r;
    }

    //get monday of the week
}
